##########################################################################################

library(Seurat)
library(data.table)
library(optparse)
library(ggplot2)
library(dplyr)
library(RColorBrewer)

##########################################################################################

option_list <- list(
    make_option(c("--singleCell_sample_file"), type = "character"),
    make_option(c("--single_cell_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){

    singleCell_sample_file <- "~/20220915_gastric_multiple/dna_combinePublic/config/singleCell_Sample.useThree.list"
    single_cell_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/singleCell/njmu/ALL_SIN_celltype.Rdata"
    out_path <- "~/20220915_gastric_multiple/dna_combinePublic/finalPlot/revise/im_favored"

}

##########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

out_path <- opt$out_path
singleCell_sample_file <- opt$singleCell_sample_file
single_cell_file <- opt$single_cell_file

dir.create(out_path , recursive = T)

##########################################################################################

info_singlecell <- data.frame(fread(singleCell_sample_file))
sc_dataset <- load(single_cell_file, verbose = F)

##########################################################################################

sc_dataset_all <- ALL_SIN_celltype

##Idents函数定义要取的类别
Idents(sc_dataset_all) <- "sample"   

##########################################################################################
## 提取最后纳入分析的样本
## 3个
info_singlecell <- subset( info_singlecell , singlecell_ID != "" )
patientid <- unique(substring(unique(info_singlecell$singlecell_ID) , 0 , 5))
## 提前用到样本的细胞
im_sc_dataset <- subset(sc_dataset_all , patient %in% patientid & sample == 'IM' & celltype %in% c("Bcell" , "Mast" , "Tcell"))

##Idents函数定义要取的类别
Idents(sc_dataset_all) <- "sample"   

##########################################################################################
## 提取用到的样本
## 从新聚类
sc_dataset_im <- im_sc_dataset
sc_dataset_im <- FindNeighbors(sc_dataset_im, reduction = "pca", dims = 1:30)
sc_dataset_im <- FindClusters(sc_dataset_im , graph.name = grep( "snn" , names(sc_dataset_im@graphs) , value = T ) , resolution = 0.6)
sc_dataset_im <- RunUMAP(sc_dataset_im, dims = 1:10)

##########################################################################################
## 染色
marker_genes <- c(
	"MZB1", #Plasma
    "MS4A1", #Naive
    "TPSB2" , "CPA3", # Mast 
	"CD8A", "GZMA", "GZMB" , # CD8 T
    "IL2RA" , "FOXP3" , # Treg 调节性T细胞
    "CCR7","TCF7","SELL","LEF1", # Tcm  Naïve  or central memory  T cell
    "KLRB1","IL17A" # Th17	辅助性T细胞17
    )

## 细胞类型
p1 <- DimPlot(sc_dataset_im, reduction = 'umap', 
	group.by = 'celltype1' , pt.size = 1.2, order = c(2,1)) +
    theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 12,color="black",face='bold'),
        legend.position = 'right' ,
        axis.text.x = element_text(size = 10,color="black",face='bold'),
        axis.text.y = element_text(size = 10,color="black",face='bold'),
        axis.title.x = element_text(size = 10,color="black",face='bold'),
        axis.title.y = element_text(size = 13,color="black",face='bold'),
        strip.text.x = element_text(size = 15,color="black",face='bold'),
        axis.ticks.length = unit(0.2, "cm") ,
        axis.line = element_line(size = 0.5))

out_name <- paste0( out_path , "/umap_celltype.immunecell.pdf"  )
ggsave( out_name , p1 ,width = 6 , height = 6)

## 样本来源
p2 <- DimPlot(sc_dataset_im, reduction = 'umap', 
	group.by = 'patient' , pt.size = 1.2, order = c(2,1)) +
    theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 12,color="black",face='bold'),
        legend.position = 'right' ,
        axis.text.x = element_text(size = 10,color="black",face='bold'),
        axis.text.y = element_text(size = 10,color="black",face='bold'),
        axis.title.x = element_text(size = 10,color="black",face='bold'),
        axis.title.y = element_text(size = 13,color="black",face='bold'),
        strip.text.x = element_text(size = 15,color="black",face='bold'),
        axis.ticks.length = unit(0.2, "cm") ,
        axis.line = element_line(size = 0.5))
out_name <- paste0( out_path , "/umap_sample.immunecell.pdf"  )
ggsave( out_name , p2 ,width = 6 , height = 6)


## marker基因
p3 <- FeaturePlot( sc_dataset_im , features = marker_genes, pt.size = 0.2 , cols= c("grey","red") ) 
out_name <- paste0( out_path , "/umap_markers.immunecell.pdf"  )
ggsave( out_name , p3 ,width = 9 , height = 7)
